import { _decorator, Component, Node, SpringJoint2D, Sprite, UITransform,Animation, AnimationClip, animation, SpriteFrame } from 'cc';
import EventManager from '../../Runtime/EventManger';
import { CONTROLLER_ENUM, DIRECTION_ENUM, DIRECTION_ORDER_ENUM, ENTITY_TYPE_ENUM, ENTITY_STATE_ENUM, EVENT_ENUM, PARAMS_NAME_ENUM, SHAKE_TYPE_ENUM } from '../../Enums';
import { TILE_HEIGHT, TILE_WIDTH, TileManager } from '../Tile/TileManager';
import ResourceManager from '../../Runtime/ResourceManager';
import { PlayerStateMachine } from './PlayerStateMachine';
import { EntityManager} from '../../Base/EntityManager';
import DataManager from '../../Runtime/DataManager';
import { IEntity } from '../../levels';
import { EnemyManager } from '../../Base/EnemyManager';
import { BurstManager } from '../Burst/BurstManager';
const { ccclass, property } = _decorator;

enum MOVE_DIRECTION_ENUM{
  TOP='TOP',
  BOTTOM='BOTTOM',
  LEFT='LEFT',
  RIGHT='RIGHT'
}

interface Pos{
  x:number,
  y:number
}

@ccclass('PlayerManager')
export class PlayerManager extends EntityManager {
    targetX: number = 0                     // 目标X坐标
    targetY: number = 0                     // 目标Y坐标
    isMoving: boolean = false            // 是否正在移动
    private readonly speed: number = 1/10   // 移动速度

    // 初始化
    async init(params:IEntity) {
      // 3. 初始化状态机
      this.fsm = this.addComponent(PlayerStateMachine)
      await this.fsm.init()
      super.init(params)  // 调用父类初始化
      this.targetX=this.x
      this.targetY=this.y

      // 4. 注册事件监听
      EventManager.Instance.on(EVENT_ENUM.PLAYER_CTRL, this.inputHandle, this)
      EventManager.Instance.on(EVENT_ENUM.ATTACK_PLAYER,this.onDead,this)
    }

    onDestroy() {
      super.onDestroy()
      EventManager.Instance.off(EVENT_ENUM.PLAYER_CTRL, this.inputHandle)
      EventManager.Instance.off(EVENT_ENUM.ATTACK_PLAYER,this.onDead)
    }

    update() {
      this.updateXY()  // 更新坐标
      // 设置节点位置（包含偏移计算）
      super.update()
    }

    updateXY(){
      if(this.targetX<this.x){
        this.x-=this.speed
      }else if(this.targetX>this.x){
        this.x+=this.speed
      }

      if(this.targetY<this.y){
        this.y-=this.speed
      }else if(this.targetY>this.y){
        this.y+=this.speed
      }

      if(Math.abs(this.targetX-this.x)<=0.1&&Math.abs(this.targetY-this.y)<=0.1&&this.isMoving){
        this.isMoving=false
        this.x=this.targetX
        this.y=this.targetY
        EventManager.Instance.emit(EVENT_ENUM.PLAYER_MOVE_END)
      }
    }

    onDead(type:ENTITY_STATE_ENUM){
      this.state=type
    }

    onAttackShake(type:SHAKE_TYPE_ENUM){
      EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE,type)
    }

    inputHandle(inputDirection: CONTROLLER_ENUM){
      if(this.isMoving){
        return
      }
      if(this.state===ENTITY_STATE_ENUM.DEATH||this.state===ENTITY_STATE_ENUM.AIRDEATH||this.state===ENTITY_STATE_ENUM.ATTACK){
        return
      }

      const id=this.willAttack(inputDirection)
      if(id){
        console.log('攻击，id:'+id);
        EventManager.Instance.emit(EVENT_ENUM.RECORD_STEP)
        // this.state=ENTITY_STATE_ENUM.ATTACK
        EventManager.Instance.emit(EVENT_ENUM.ATTACK_ENENY,id)
        EventManager.Instance.emit(EVENT_ENUM.DoorOpen)
        EventManager.Instance.emit(EVENT_ENUM.PLAYER_MOVE_END)
        return
      }

      if(this.willBlock(inputDirection)){
        console.log('前方有阻挡，无法移动');
        if(inputDirection===CONTROLLER_ENUM.TOP){
          EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE,SHAKE_TYPE_ENUM.TOP)
        }else if(inputDirection===CONTROLLER_ENUM.BOTTOM){
          EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE,SHAKE_TYPE_ENUM.BOTTOM)
        }else if(inputDirection===CONTROLLER_ENUM.LEFT){
          EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE,SHAKE_TYPE_ENUM.LEFT)
        }else if(inputDirection===CONTROLLER_ENUM.RIGHT){
          EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE,SHAKE_TYPE_ENUM.RIGHT)
        }else if(inputDirection===CONTROLLER_ENUM.TURNLEFT){
          if(this.direction===DIRECTION_ENUM.TOP){
            EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE,SHAKE_TYPE_ENUM.LEFT)
          }else if(this.direction===DIRECTION_ENUM.BOTTOM){
            EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE,SHAKE_TYPE_ENUM.RIGHT)
          }else if(this.direction===DIRECTION_ENUM.LEFT){
            EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE,SHAKE_TYPE_ENUM.BOTTOM)
          }else if(this.direction===DIRECTION_ENUM.RIGHT){
            EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE,SHAKE_TYPE_ENUM.TOP)
          }
        }else if(inputDirection===CONTROLLER_ENUM.TURNRIGHT){
          if(this.direction===DIRECTION_ENUM.TOP){
            EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE,SHAKE_TYPE_ENUM.RIGHT)
          }else if(this.direction===DIRECTION_ENUM.BOTTOM){
            EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE,SHAKE_TYPE_ENUM.LEFT)
          }else if(this.direction===DIRECTION_ENUM.LEFT){
            EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE,SHAKE_TYPE_ENUM.TOP)
          }else if(this.direction===DIRECTION_ENUM.RIGHT){
            EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE,SHAKE_TYPE_ENUM.BOTTOM)
          }
        }
        return
      }
      this.move(inputDirection)
    }

    move(inputDirection: CONTROLLER_ENUM) {
      EventManager.Instance.emit(EVENT_ENUM.RECORD_STEP)
      console.log(this.x,this.y);
      // 方向移动
      if (inputDirection === CONTROLLER_ENUM.TOP) {
        this.targetY -= 1
        this.isMoving=true
        this.showSmoke(DIRECTION_ENUM.TOP)
      } else if (inputDirection === CONTROLLER_ENUM.BOTTOM) {
        this.targetY += 1
        this.isMoving=true
        this.showSmoke(DIRECTION_ENUM.BOTTOM)
      } else if (inputDirection === CONTROLLER_ENUM.LEFT) {
        this.targetX -= 1
        this.isMoving=true
        this.showSmoke(DIRECTION_ENUM.LEFT)
      } else if (inputDirection === CONTROLLER_ENUM.RIGHT) {
        this.targetX += 1
        this.isMoving=true
        this.showSmoke(DIRECTION_ENUM.RIGHT)
      }
      // 状态切换
      else if (inputDirection === CONTROLLER_ENUM.TURNLEFT) {
        if(this.direction===DIRECTION_ENUM.TOP){
          this.direction=DIRECTION_ENUM.LEFT
        }else if(this.direction===DIRECTION_ENUM.LEFT){
          this.direction=DIRECTION_ENUM.BOTTOM
        }else if(this.direction===DIRECTION_ENUM.BOTTOM){
          this.direction=DIRECTION_ENUM.RIGHT
        }else if(this.direction===DIRECTION_ENUM.RIGHT){
          this.direction=DIRECTION_ENUM.TOP
        }
        this.state=ENTITY_STATE_ENUM.TURNLEFT
        EventManager.Instance.emit(EVENT_ENUM.PLAYER_MOVE_END)
      } else if (inputDirection === CONTROLLER_ENUM.TURNRIGHT) {
        if(this.direction===DIRECTION_ENUM.TOP){
          this.direction=DIRECTION_ENUM.RIGHT
        }else if(this.direction===DIRECTION_ENUM.RIGHT){
          this.direction=DIRECTION_ENUM.BOTTOM
        }else if(this.direction===DIRECTION_ENUM.BOTTOM){
          this.direction=DIRECTION_ENUM.LEFT
        }else if(this.direction===DIRECTION_ENUM.LEFT){
          this.direction=DIRECTION_ENUM.TOP
        }
        this.state=ENTITY_STATE_ENUM.TURNRIGHT
        EventManager.Instance.emit(EVENT_ENUM.PLAYER_MOVE_END)
      }
    }

    showSmoke(type:DIRECTION_ENUM){
      EventManager.Instance.emit(EVENT_ENUM.SHOW_SMOKE,this.x,this.y,type)
    }

    willAttack(inputDirection: CONTROLLER_ENUM){
      const enemies=DataManager.Instance.enemies.filter(enemy=>enemy.state !==ENTITY_STATE_ENUM.DEATH)
      for(let i=0;i<enemies.length;++i){
        const {x:enemyX,y:enemyY,id:enemyId}=enemies[i]
        if(//top攻击
          inputDirection===CONTROLLER_ENUM.TOP&&
          this.direction===DIRECTION_ENUM.TOP&&
          enemyX===this.x&&
          enemyY===this.targetY-2
        ){
          this.state=ENTITY_STATE_ENUM.ATTACK
          return enemyId
        }

        if(//bottom攻击
          inputDirection===CONTROLLER_ENUM.BOTTOM&&
          this.direction===DIRECTION_ENUM.BOTTOM&&
          enemyX===this.x&&
          enemyY===this.targetY+2
        ){
          this.state=ENTITY_STATE_ENUM.ATTACK
          return enemyId
        }

        if(//left攻击
          inputDirection===CONTROLLER_ENUM.LEFT&&
          this.direction===DIRECTION_ENUM.LEFT&&
          enemyX===this.targetX-2&&
          enemyY===this.y
        ){
          this.state=ENTITY_STATE_ENUM.ATTACK
          return enemyId
        }

        if(//left攻击
          inputDirection===CONTROLLER_ENUM.RIGHT&&
          this.direction===DIRECTION_ENUM.RIGHT&&
          enemyX===this.targetX+2&&
          enemyY===this.y
        ){
          this.state=ENTITY_STATE_ENUM.ATTACK
          return enemyId
        }
      }
      return ''
    }

    willBlock(inputDirection: CONTROLLER_ENUM){
      // 添加参数验证
      // if (!inputDirection) {
      //   console.error('inputDirection is undefined');
      //   return true;
      // }
      const{targetX:x,targetY:y,direction}=this
      const {tileInfo}=DataManager.Instance

      // 添加数据完整性检查
      // if (!tileInfo || !Array.isArray(tileInfo) || tileInfo.length === 0) {
      //   console.error('tileInfo is not properly initialized');
      //   return true;
      // }

      // 添加坐标有效性检查
      // if (x === undefined || y === undefined ||
      //     x < 0 || x >= tileInfo.length ||
      //     y < 0 || y >= (tileInfo[0]?.length || 0)) {
      //   console.error(`Invalid coordinates: x=${x}, y=${y}`);
      //   return true;
      // }

      const {x:doorX,y:doorY,state:doorState}=DataManager.Instance.door
      const enemies:EnemyManager[]=DataManager.Instance.enemies.filter((enemy:EnemyManager)=>enemy.state!=ENTITY_STATE_ENUM.DEATH)
      const bursts:BurstManager[]=DataManager.Instance.bursts.filter((burst:BurstManager)=>burst.state!=ENTITY_STATE_ENUM.DEATH)

      let playerNext:Pos={x,y}//角色下一个位置
      let weaponNext:Pos={x,y}//武器下一个位置
      let playerNextTile:TileManager//角色下一个位置瓦片信息
      let weaponNextTile:TileManager//武器下一个位置瓦片信息

      if(inputDirection===CONTROLLER_ENUM.TOP){//角色控制向上移动
        playerNext=this.moveDirection(playerNext,MOVE_DIRECTION_ENUM.TOP)
        weaponNext=this.moveDirection(weaponNext,MOVE_DIRECTION_ENUM.TOP)

        if(direction===DIRECTION_ENUM.TOP){//角色方向上
          weaponNext=this.moveDirection(weaponNext,MOVE_DIRECTION_ENUM.TOP)
        }else if(direction===DIRECTION_ENUM.LEFT){//角色方向左
          weaponNext=this.moveDirection(weaponNext,MOVE_DIRECTION_ENUM.LEFT)
        }else if(direction===DIRECTION_ENUM.BOTTOM){//角色方向下
          weaponNext=this.moveDirection(weaponNext,MOVE_DIRECTION_ENUM.BOTTOM)
        }else if(direction===DIRECTION_ENUM.RIGHT){//角色方向右
          weaponNext=this.moveDirection(weaponNext,MOVE_DIRECTION_ENUM.RIGHT)
        }

        //判断是否超出边界
        if(playerNext.y<0||playerNext.x<0){
          this.state=ENTITY_STATE_ENUM.BLOCKFRONT
          return true
        }

        playerNextTile=tileInfo[playerNext.x]&&tileInfo[playerNext.x][playerNext.y]
        weaponNextTile=tileInfo[weaponNext.x]&&tileInfo[weaponNext.x][weaponNext.y]

        //移动方向有门
        if((doorX===playerNext.x&&doorY===playerNext.y)||(doorX===weaponNext.x&&doorY===weaponNext.y)){
          //门存在
          if(doorState!==ENTITY_STATE_ENUM.DEATH){
            this.state=ENTITY_STATE_ENUM.BLOCKFRONT
            return true
          }
        }
        //判断敌人
        for(let i=0;i<enemies.length;++i){
          const {x:enemyX,y:enemyY}=enemies[i]
          if((enemyX===playerNext.x&&enemyY===playerNext.y)||(enemyX===weaponNext.x&&enemyY===weaponNext.y)){
            this.state=ENTITY_STATE_ENUM.BLOCKFRONT
            return true
          }
        }
        //判断陷阱
        for(let i=0;i<bursts.length;++i){
          const {x:burstX,y:burstY}=bursts[i]
          if(playerNext.x===burstX&&playerNext.y===burstY&&(!weaponNextTile||weaponNextTile.turnable)){
            return false
          }
        }
        //判断移动方向是否锁定
        if(playerNextTile&&playerNextTile.moveable&&(!weaponNextTile||weaponNextTile.turnable)){
            //empty
        }else{
          this.state=ENTITY_STATE_ENUM.BLOCKFRONT
          return true
        }
      }else if(inputDirection===CONTROLLER_ENUM.BOTTOM){//角色控制向下移动
        playerNext=this.moveDirection(playerNext,MOVE_DIRECTION_ENUM.BOTTOM)
        weaponNext=this.moveDirection(weaponNext,MOVE_DIRECTION_ENUM.BOTTOM)

        if(direction===DIRECTION_ENUM.TOP){//角色方向上
          weaponNext=this.moveDirection(weaponNext,MOVE_DIRECTION_ENUM.TOP)
        }else if(direction===DIRECTION_ENUM.LEFT){//角色方向左
          weaponNext=this.moveDirection(weaponNext,MOVE_DIRECTION_ENUM.LEFT)
        }else if(direction===DIRECTION_ENUM.BOTTOM){//角色方向下
          weaponNext=this.moveDirection(weaponNext,MOVE_DIRECTION_ENUM.BOTTOM)
        }else if(direction===DIRECTION_ENUM.RIGHT){//角色方向右
          weaponNext=this.moveDirection(weaponNext,MOVE_DIRECTION_ENUM.RIGHT)
        }

        //判断是否超出边界
        if(playerNext.y<0||playerNext.x<0){
          this.state=ENTITY_STATE_ENUM.BLOCKBACK
          return true
        }

        playerNextTile=tileInfo[playerNext.x]&&tileInfo[playerNext.x][playerNext.y]
        weaponNextTile=tileInfo[weaponNext.x]&&tileInfo[weaponNext.x][weaponNext.y]

        //移动方向有门
        if((doorX===playerNext.x&&doorY===playerNext.y)||(doorX===weaponNext.x&&doorY===weaponNext.y)){
          //门存在
          if(doorState!==ENTITY_STATE_ENUM.DEATH){
            this.state=ENTITY_STATE_ENUM.BLOCKFRONT
            return true
          }
        }

        for(let i=0;i<enemies.length;++i){
          const {x:enemyX,y:enemyY}=enemies[i]
          if((enemyX===playerNext.x&&enemyY===playerNext.y)||(enemyX===weaponNext.x&&enemyY===weaponNext.y)){
            this.state=ENTITY_STATE_ENUM.BLOCKFRONT
            return true
          }
        }

        //判断陷阱
        for(let i=0;i<bursts.length;++i){
          const {x:burstX,y:burstY}=bursts[i]
          if(playerNext.x===burstX&&playerNext.y===burstY&&(!weaponNextTile||weaponNextTile.turnable)){
            return false
          }
        }

        //判断移动方向是否锁定
        if(playerNextTile&&playerNextTile.moveable&&(!weaponNextTile||weaponNextTile.turnable)){
            //empty
        }else{
          this.state=ENTITY_STATE_ENUM.BLOCKBACK
          return true
        }
      }else if(inputDirection===CONTROLLER_ENUM.LEFT){//角色控制向左移动
        playerNext=this.moveDirection(playerNext,MOVE_DIRECTION_ENUM.LEFT)
        weaponNext=this.moveDirection(weaponNext,MOVE_DIRECTION_ENUM.LEFT)

        if(direction===DIRECTION_ENUM.TOP){//角色方向上
          weaponNext=this.moveDirection(weaponNext,MOVE_DIRECTION_ENUM.TOP)
        }else if(direction===DIRECTION_ENUM.LEFT){//角色方向左
          weaponNext=this.moveDirection(weaponNext,MOVE_DIRECTION_ENUM.LEFT)
        }else if(direction===DIRECTION_ENUM.BOTTOM){//角色方向下
          weaponNext=this.moveDirection(weaponNext,MOVE_DIRECTION_ENUM.BOTTOM)
        }else if(direction===DIRECTION_ENUM.RIGHT){//角色方向右
          weaponNext=this.moveDirection(weaponNext,MOVE_DIRECTION_ENUM.RIGHT)
        }

        //判断是否超出边界
        if(playerNext.y<0||playerNext.x<0){
          this.state=ENTITY_STATE_ENUM.BLOCKLEFT
          return true
        }

        playerNextTile=tileInfo[playerNext.x]&&tileInfo[playerNext.x][playerNext.y]
        weaponNextTile=tileInfo[weaponNext.x]&&tileInfo[weaponNext.x][weaponNext.y]

        //移动方向有门
        if((doorX===playerNext.x&&doorY===playerNext.y)||(doorX===weaponNext.x&&doorY===weaponNext.y)){
          //门存在
          if(doorState!==ENTITY_STATE_ENUM.DEATH){
            this.state=ENTITY_STATE_ENUM.BLOCKFRONT
            return true
          }
        }

        for(let i=0;i<enemies.length;++i){
          const {x:enemyX,y:enemyY}=enemies[i]
          if((enemyX===playerNext.x&&enemyY===playerNext.y)||(enemyX===weaponNext.x&&enemyY===weaponNext.y)){
            this.state=ENTITY_STATE_ENUM.BLOCKFRONT
            return true
          }
        }

        //判断陷阱
        for(let i=0;i<bursts.length;++i){
          const {x:burstX,y:burstY}=bursts[i]
          if(playerNext.x===burstX&&playerNext.y===burstY&&(!weaponNextTile||weaponNextTile.turnable)){
            return false
          }
        }

        //判断移动方向是否锁定
        if(playerNextTile&&playerNextTile.moveable&&(!weaponNextTile||weaponNextTile.turnable)){
            //empty
        }else{
          this.state=ENTITY_STATE_ENUM.BLOCKLEFT
          return true
        }
      }else if(inputDirection===CONTROLLER_ENUM.RIGHT){//角色控制右移动
        playerNext=this.moveDirection(playerNext,MOVE_DIRECTION_ENUM.RIGHT)
        weaponNext=this.moveDirection(weaponNext,MOVE_DIRECTION_ENUM.RIGHT)

        if(direction===DIRECTION_ENUM.TOP){//角色方向上
          weaponNext=this.moveDirection(weaponNext,MOVE_DIRECTION_ENUM.TOP)
        }else if(direction===DIRECTION_ENUM.LEFT){//角色方向左
          weaponNext=this.moveDirection(weaponNext,MOVE_DIRECTION_ENUM.LEFT)
        }else if(direction===DIRECTION_ENUM.BOTTOM){//角色方向下
          weaponNext=this.moveDirection(weaponNext,MOVE_DIRECTION_ENUM.BOTTOM)
        }else if(direction===DIRECTION_ENUM.RIGHT){//角色方向右
          weaponNext=this.moveDirection(weaponNext,MOVE_DIRECTION_ENUM.RIGHT)
        }

        //判断是否超出边界
        if(playerNext.y<0||playerNext.x<0){
          this.state=ENTITY_STATE_ENUM.BLOCKLEFT
          return true
        }

        playerNextTile=tileInfo[playerNext.x]&&tileInfo[playerNext.x][playerNext.y]
        weaponNextTile=tileInfo[weaponNext.x]&&tileInfo[weaponNext.x][weaponNext.y]

        //移动方向有门
        if((doorX===playerNext.x&&doorY===playerNext.y)||(doorX===weaponNext.x&&doorY===weaponNext.y)){
          //门存在
          if(doorState!==ENTITY_STATE_ENUM.DEATH){
            this.state=ENTITY_STATE_ENUM.BLOCKFRONT
            return true
          }
        }

        for(let i=0;i<enemies.length;++i){
          const {x:enemyX,y:enemyY}=enemies[i]
          if((enemyX===playerNext.x&&enemyY===playerNext.y)||(enemyX===weaponNext.x&&enemyY===weaponNext.y)){
            this.state=ENTITY_STATE_ENUM.BLOCKFRONT
            return true
          }
        }

        //判断陷阱
        for(let i=0;i<bursts.length;++i){
          const {x:burstX,y:burstY}=bursts[i]
          if(playerNext.x===burstX&&playerNext.y===burstY&&(!weaponNextTile||weaponNextTile.turnable)){
            return false
          }
        }

        //判断移动方向是否锁定
        if(playerNextTile&&playerNextTile.moveable&&(!weaponNextTile||weaponNextTile.turnable)){
            //empty
        }else{
          this.state=ENTITY_STATE_ENUM.BLOCKLEFT
          return true
        }
      }else if(inputDirection===CONTROLLER_ENUM.TURNLEFT){
        let nextX
        let nextY
        if(direction===DIRECTION_ENUM.TOP){
          nextX=x-1
          nextY=y-1
        }else if(direction===DIRECTION_ENUM.LEFT){
          nextX=x-1
          nextY=y+1
        }else if(direction===DIRECTION_ENUM.BOTTOM){
          nextX=x+1
          nextY=y+1
        }else if(direction===DIRECTION_ENUM.RIGHT){
          nextX=x+1
          nextY=y-1
        }

        //左转方向有门
        if(
          (doorX===nextX&&doorY===nextY)||
          (doorX===nextX&&doorY===y)||
          (doorX===x&&doorY===nextY)
        ){
          //门存在
          if(doorState!==ENTITY_STATE_ENUM.DEATH){
            this.state=ENTITY_STATE_ENUM.BLOCKFRONT
            return true
          }
        }

        //判断敌人
        for(let i=0;i<enemies.length;++i){
          const {x:enemyX,y:enemyY}=enemies[i]
          if(
            (enemyX===nextX&&enemyY===nextY)||
            (enemyX===nextX&&enemyY===y)||
            (enemyX===x&&enemyY===nextY)
          ){
            this.state=ENTITY_STATE_ENUM.BLOCKFRONT
            return true
          }
        }

        if(
          (!tileInfo[x][nextY]||tileInfo[x][nextY].turnable)&&
          (!tileInfo[nextX][y]||tileInfo[nextX][y].turnable)&&
          (!tileInfo[nextX][nextY]||tileInfo[nextX][nextY].turnable)
        ){
          //empty
        }else{
          console.log('不可以左转');
          this.state=ENTITY_STATE_ENUM.BLOCKTURNLEFT
          return true
        }
      }else if(inputDirection===CONTROLLER_ENUM.TURNRIGHT){
        let nextX
        let nextY
        if(direction===DIRECTION_ENUM.TOP){
          nextX=x+1
          nextY=y-1
        }else if(direction===DIRECTION_ENUM.LEFT){
          nextX=x-1
          nextY=y-1
        }else if(direction===DIRECTION_ENUM.BOTTOM){
          nextX=x-1
          nextY=y+1
        }else if(direction===DIRECTION_ENUM.RIGHT){
          nextX=x+1
          nextY=y+1
        }

        //右转方向有门
        if(
          (doorX===nextX&&doorY===nextY)||
          (doorX===nextX&&doorY===y)||
          (doorX===x&&doorY===nextY)
        ){
          //门存在
          if(doorState!==ENTITY_STATE_ENUM.DEATH){
            this.state=ENTITY_STATE_ENUM.BLOCKFRONT
            return true
          }
        }

        //判断敌人
        for(let i=0;i<enemies.length;++i){
          const {x:enemyX,y:enemyY}=enemies[i]
          if(
            (enemyX===nextX&&enemyY===nextY)||
            (enemyX===nextX&&enemyY===y)||
            (enemyX===x&&enemyY===nextY)
          ){
            this.state=ENTITY_STATE_ENUM.BLOCKFRONT
            return true
          }
        }

        if(
          (!tileInfo[x][nextY]||tileInfo[x][nextY].turnable)&&
          (!tileInfo[nextX][y]||tileInfo[nextX][y].turnable)&&
          (!tileInfo[nextX][nextY]||tileInfo[nextX][nextY].turnable)
        ){
          //empty
        }else{
          console.log('不可以右转');
          this.state=ENTITY_STATE_ENUM.BLOCKTURNRIGHT
          return true
        }
      }
      return false
    }

    moveDirection(pos:Pos,move_direction:MOVE_DIRECTION_ENUM,step:number=1):Pos{
      if(move_direction===MOVE_DIRECTION_ENUM.TOP){
        pos.y--
      }else if(move_direction===MOVE_DIRECTION_ENUM.BOTTOM){
        pos.y++
      }else if(move_direction===MOVE_DIRECTION_ENUM.LEFT){
        pos.x--
      }else if(move_direction===MOVE_DIRECTION_ENUM.RIGHT){
        pos.x++
      }
      return pos
    }
    // async render(){
    //   // 添加Sprite组件用于显示角色图片
    //   const sprite=this.addComponent(Sprite)
    //   // 设置精灵尺寸模式为自定义，允许手动设置尺寸
    //   sprite.sizeMode=Sprite.SizeMode.CUSTOM

    //   // 获取UI变换组件，用于控制节点的尺寸和位置
    //   const transform=this.getComponent(UITransform)
    //   // 设置玩家角色尺寸为4个瓦片的宽度和高度
    //   transform.setContentSize(TILE_WIDTH*4,TILE_HEIGHT*4)

    //   //加载动画资源 - 从指定路径加载玩家闲置状态的面朝上动画帧
    //   const spriteFrames=await ResourceManager.Instance.loadDir('texture/player/idle/top')

    //   //添加动画组件用于播放动画
    //   const animationComponent=this.addComponent(Animation)

    //   // 创建动画剪辑对象，用于存储动画数据
    //   const animationClip = new AnimationClip();

    //   // 创建对象轨道，用于控制Sprite组件的属性变化
    //   const track  = new animation.ObjectTrack();
    //   // 设置轨道路径：指向Sprite组件的spriteFrame属性，通过改变这个属性实现帧动画
    //   track.path = new animation.TrackPath().toComponent(Sprite).toProperty('spriteFrame');
    //   // 构建关键帧数据：将精灵帧数组转换为时间轴上的关键帧，每个帧在特定时间点显示
    //   const frames: Array<[number, SpriteFrame]> =spriteFrames.map((item, index) =>[index * ANIMATION_SPEED, item]);
    //   // 将排序后的关键帧数据赋值给动画曲线
    //   track.channel.curve.assignSorted(frames);

    //   // 将配置好的轨道添加到动画剪辑中
    //   animationClip.addTrack(track);

    //   // 设置动画剪辑的总时长 = 帧数 × 每帧持续时间
    //   animationClip.duration = frames.length*ANIMATION_SPEED;
    //   // 设置动画循环播放模式
    //   animationClip.wrapMode=AnimationClip.WrapMode.Loop
    //   // 将动画剪辑设置为动画组件的默认剪辑
    //   animationComponent.defaultClip=animationClip
    //   // 开始播放动画
    //   animationComponent.play()
    // }
}


